package com.wmlive.hhvideo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import cn.wmlive.hhvideo.R;

/**
 * Created by lsq on 7/3/2017.
 * 首页顶部的tab
 */

public class MainTopTabView extends BaseCustomView {
    @BindView(R.id.ivBell)
    ImageView ivBell;
    @BindView(R.id.tvFollow)
    TextView tvFollow;
    @BindView(R.id.tvRecommend)
    TextView tvRecommend;
    @BindView(R.id.tvLatest)
    TextView tvLatest;
    @BindView(R.id.ivDiscovery)
    ImageView ivDiscovery;
    @BindView(R.id.viewBellDot)
    View viewBellDot;
    @BindView(R.id.viewDiscoveryDot)
    View viewDiscoveryDot;
    @BindView(R.id.rl_bell)
    RelativeLayout rl_bell;



    public MainTopTabView(Context context) {
        super(context);
    }

    public MainTopTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initViews(Context context, AttributeSet attrs, int defStyle) {
        ivBell.setOnClickListener(this);
        tvFollow.setOnClickListener(this);
        tvRecommend.setOnClickListener(this);
        tvLatest.setOnClickListener(this);
        ivDiscovery.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_main_top_tab;
    }

    @Override
    protected void onSingleClick(View v) {
        super.onSingleClick(v);
        if (null != onMainTabClickListener) {
            boolean isValid;
            switch (v.getId()) {
                case R.id.ivBell:
                    onMainTabClickListener.onBellClick();
                    break;
                case R.id.tvFollow:
                    isValid = onMainTabClickListener.onFollowClick();
                    if (isValid) {
                        setSelect(0);
                    }
                    break;
                case R.id.tvRecommend:
                    isValid = onMainTabClickListener.onRecommendClick();
                    if (isValid) {
                        setSelect(1);
                    }
                    break;
                case R.id.tvLatest:
                    isValid = onMainTabClickListener.onLatestClick();
                    if (isValid) {
                        setSelect(2);
                    }
                    break;
                case R.id.ivDiscovery:
                    onMainTabClickListener.onDiscoveryClick();
                    break;
            }
        }
    }

    public void setSelect(int position) {
        tvFollow.setTextAppearance(getContext(), position == 0 ? R.style.tv_a_32 : R.style.tv_a_28_a70);
        tvRecommend.setTextAppearance(getContext(), position == 1 ? R.style.tv_a_32 : R.style.tv_a_28_a70);
        tvLatest.setTextAppearance(getContext(), position == 2 ? R.style.tv_a_32 : R.style.tv_a_28_a70);
        tvFollow.getPaint().setFakeBoldText(position == 0);
        tvRecommend.getPaint().setFakeBoldText(position == 1);
        tvLatest.getPaint().setFakeBoldText(position == 2);
    }


    /**
     * 是否显示铃铛
     *
     * @param show
     */
    public void showBell(boolean show) {
        ivBell.setVisibility(show ? VISIBLE : GONE);
        if (!show) {
            showBellDot(show);
        }
    }

    /**
     * 是否显示铃铛的小红点
     *
     * @param show true显示
     */
    public void showBellDot(boolean show) {
        viewBellDot.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * 是否显示发现的小红点
     *
     * @param show true显示
     */
    public void showDiscoverDot(boolean show) {
        viewDiscoveryDot.setVisibility(show ? VISIBLE : GONE);
    }

    private OnMainTabClickListener onMainTabClickListener;

    public void setTabClickListener(OnMainTabClickListener onMainTabClickListener) {
        this.onMainTabClickListener = onMainTabClickListener;
    }

    /**
     * 显示铃铛
     */
    public void showRLBellView(){
        if(rl_bell!=null && rl_bell.getVisibility()!=View.VISIBLE){
            rl_bell.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 隐藏铃铛
     */
    public void hideRLBellView(){
        if(rl_bell!=null && rl_bell.getVisibility()!=View.GONE){
            rl_bell.setVisibility(View.GONE);
        }
    }
}
